package ming.util.base;

import static ming.util.base.HqlFilter.getSqlOperator;

import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import ming.model.base.Organization;
import ming.model.base.SessionInfo;
import ming.model.base.User;
import ming.service.base.OrganizationServiceI;

@Component
public class RoleUtils {

    @Autowired
    HttpSession session;
    @Autowired
    HttpServletRequest request;


    @Autowired
    private OrganizationServiceI organizationServiceI;

    public String getHaveRolePrintType(boolean real) {
        SessionInfo sessionInfo = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
        User user = sessionInfo.getUser();
        Set<Organization> organizations;
        if (user.getLoginname().equals("mingsuper")&&real==false) {
            organizations = new HashSet<>(organizationServiceI.findByFilter(new HqlFilter()));
        } else {
            organizations = user.getOrganizations();
        }
        HqlFilter ohqlFilter = new HqlFilter();
        StringBuilder sb = new StringBuilder();

        String realPrinttype = "";
        String printtypes;
        String username = user.getLoginname();
        printtypes = session.getAttribute("printtypes").toString();

        for (String str : printtypes.split(",")) {
            if (sb.toString().contains(str) || ("mingsuper".equals(username)&&real==false)) {
                realPrinttype += str + ",";
            }
        }
        return realPrinttype;
    }

    public String getOrganization() {

        SessionInfo sessionInfo = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
        User user = sessionInfo.getUser();
        Set<Organization> organizations = user.getOrganizations();
        StringBuilder sb = new StringBuilder();
        HqlFilter ohqlFilter = new HqlFilter();

        for (Organization organization : organizations) {
            ohqlFilter = new HqlFilter();
            ohqlFilter.addFilter("QUERY_t#id_S_EQ", organization.getId());
            Organization temp = organizationServiceI.getByFilter(ohqlFilter);
            if (temp.getOrganizations() != null && temp.getOrganizations().size() > 0) {
                for (Organization tempOrganization : temp.getOrganizations()) {
                    sb.append(tempOrganization.getName() + ",");
                }
            }
        }
        return sb.toString();
    }

    public String getHaveRoleStudent(String tableName, String id) {
        SessionInfo sessionInfo = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
        User user = sessionInfo.getUser();
        Set<Organization> organizations = user.getOrganizations();

        StringBuilder sql = new StringBuilder();
        sql.append("select t.* from " + tableName + " t left join z_v_xsjbxxb x on t." + id + "=x.xh where 1=1 ");
        int count = 0;
        for (Organization organization : organizations) {
            if (organization.getOrganization() == null) {
                String orgHql = "from Organization where ORGANIZATION_ID='" + organization.getId() + "'";
                List<Organization> organizations1 = organizationServiceI.find(orgHql);
                for (Organization organization1 : organizations1) {
                    if (count == 0) {
                        sql.append(" and (");
                    } else {
                        sql.append(" or ");
                    }
                    sql.append(" x.xymc = '" + organization1.getName() + "'");
                    count++;
                }
            } else {
                if (count == 0) {
                    sql.append(" and (");
                } else {
                    sql.append(" or ");
                }
                sql.append(" x.xymc = '" + organization.getName() + "'");
                count++;
            }
        }
        if(sql.toString().contains("("))
        {
            sql.append(" )");
        }

        Enumeration<String> names = request.getParameterNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            String value = request.getParameter(name);
            if (name.contains("QUERY_")) {
                String[] filterParams = StringUtils.split(name, "_");
                if (filterParams.length == 4) {
                    String columnName = filterParams[1].replaceAll("#", ".");
                    String operator = filterParams[3];
                    sql.append(" and " + columnName.replaceAll("t\\.", "t\\.vc") + " "
                            + getSqlOperator(operator) + " '" + value + "' ");
                }
            }

        }
        return sql.toString();
    }
}